package com.example.config.security;

import com.alibaba.fastjson.JSON;
import com.example.common.ResultBody;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Configuration
public class AuthenticationExceptionHandle implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("utf8");
        response.setContentType("application/json");
        response.setStatus(500);
        ResultBody result = new ResultBody(false, null, "登陆出现异常");
        String s = JSON.toJSONString(result);
        response.getWriter().println(s);
    }
}
